package com.robot.controller;

import com.robot.dto.RunTestContext;
import com.robot.model.BuildTestDo;
import com.robot.service.MailReportService;
import com.robot.service.RunTestService;
import com.robot.thread.ThreadRunTest;
import com.robot.vo.ReportLog;
import com.robot.vo.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/11/24
 * Time: 16:53
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/runTest")
public class RunTestController {

    @Autowired
    RunTestService runTestService;
    @Autowired
    ThreadPoolTaskExecutor executor;
    @Autowired
    MailReportService mailReportService;

    @RequestMapping("/run")
    public ModelAndView runTest(Integer buildTestId){
        /**不会等待线程执行完成，这里是一个非阻塞的调用**/
        // ThreadRunTest 是new出来的对象，它不在spring容器的上下文里面，那么autowired是无法注入的
        ThreadRunTest threadRunTest =new ThreadRunTest(buildTestId,runTestService,mailReportService);
        executor.execute(threadRunTest);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("redirect:/report/reportPage?buildTestId="+buildTestId);
        return modelAndView;
    }
    /**
     * 读取运行日志
     * **/
    @RequestMapping("/runLog")
    public @ResponseBody
    ReportLog runLog(Integer buildTestId,Long lastTimeFileSize){
        return runTestService.runLog(buildTestId,lastTimeFileSize);
    }
}
